package com.fcscs.cservice.client.main.reservation;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.reservation.RateViewModelData;
import com.fcscs.cservice.domain.model.RateView;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class RoomRateDetailDialog extends BaseFormDialog<ReservationView> {
	private MyFormPanel formPanel;
	private static RoomRateDetailDialog INSTANCE;
	private List<RateView> rateViewList;
	private boolean isFirst;
	
	public static RoomRateDetailDialog getInstance(List<RateView> rateViewList){
		if(null == INSTANCE){
			INSTANCE = new RoomRateDetailDialog();
		}
		INSTANCE.rateViewList = rateViewList;
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.roomRateDetail());
		return INSTANCE;
	}
	
	private RoomRateDetailDialog(){
		formPanel = new MyFormPanel(null);
		formPanel.setId("RoomRateDetailDialog_formPanel");
		isFirst = true;
		add(formPanel);
		setSize(360, 260);
	}

	private class MySimplePropertyGridPanel extends
			SimpleGridPanel<RateViewModelData> {
		public MySimplePropertyGridPanel(String heading,
				ModelSelectListener<RateViewModelData> l, Object... params) {
			super(heading, null, (Object[]) null);
			setCollapsible(false);
			setSize(320, 150);
		}

		@Override
		protected ColumnModel createColumnConfig(Object... params) {
			List<ColumnConfig> colcfgs = new ArrayList<ColumnConfig>();

			colcfgs.add(CoreUtil.createNonSortableColumnConf(
					ShareGWTConstants.FROM,
					CoreConstants.CORE_CONSTANTS.from(), 100));
			colcfgs.add(CoreUtil.createNonSortableColumnConf(
					ShareGWTConstants.TO, CoreConstants.CORE_CONSTANTS.to(),
					100));
			colcfgs.add(CoreUtil.createNonSortableColumnConf(
					CServiceConstants.RATE, CServiceConstants.CSERVICE_CONSTANTS.roomRate(),
					100));
			ColumnModel columnModel = new ColumnModel(colcfgs);
			return columnModel;
		}

		public void refresh() {
			myGrid.getView().refresh(true);
		}

	}
	
	private class MyFormPanel extends FcsFormPanel<ReservationView>{
		private MySimplePropertyGridPanel gridPanel;
		private FormButton okButton;
		protected MyFormPanel(FormSubmitListener<ReservationView> l) {
			super(l);
			setHeaderVisible(false);
			
			gridPanel = new MySimplePropertyGridPanel(CServiceConstants.CSERVICE_CONSTANTS.roomRateDetail(),null);
			gridPanel.setId("RoomRateDetailDialog_gridPanel");
			gridPanel.setHeaderVisible(false);
			add(gridPanel);
			
			okButton = new FormButton(CoreConstants.CORE_CONSTANTS.ok());
			okButton.setId("RoomRateDetailDialog_okButton");
//			okButton.setWidth(60);
//			okButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "240px");
//			okButton.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "10px");
			okButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					RoomRateDetailDialog.this.hide();
					gridPanel.removeAllModels();
				}
			});
			
			getButtonBar().add(okButton);
		}

		@Override
		public void updateForm(ReservationView entity) {
			gridPanel.removeAllModels();
			for(RateView rateView : rateViewList){
				gridPanel.addModel(new RateViewModelData(rateView));
			}
		}

		@Override
		public void populateEntity(ReservationView entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	protected FcsFormPanel<ReservationView> getFormPanel() {
		return formPanel;
	}

	@Override
	public void show() {
		super.show();
		formPanel.gridPanel.refresh();
	}

	@Override
	public void hide() {
		super.hide();
		if(!isFirst){
			formPanel.gridPanel.removeAllModels();
		}
		isFirst = false;
	}
}
